package com.bwt.top.bwt_ad_flutter.manager;

import android.content.Context;
import android.text.TextUtils;

import com.bwt.top.AdSdk;
import com.bwt.top.AdSdk.InitCallback;
import com.bwt.top.bwt_ad_flutter.util.LogUtil;

public class InitManager {
    private static boolean inited = false;

    private static boolean bwtInited = false;
    private static boolean bwtInitSuccess = false;
    private static InitCallback bwtInitCallback = null;

    private InitManager() {
    }

    public static boolean isInited() {
        return inited;
    }

    public static boolean isBwtInited() {
        return bwtInited;
    }

    public static boolean isBwtInitSuccess() {
        return bwtInitSuccess;
    }

    public static void setBwtInitCallback(InitCallback bwtInitCallback) {
        InitManager.bwtInitCallback = bwtInitCallback;
    }

    public static void init(Context context, String appId, String logType, String wechatAppId) {
        if (inited || context == null || TextUtils.isEmpty(appId)) return;

        // 缩短⼴告的响应，提⾼曝光率，减少特定⻚⾯⼴告“闪⼊”的情况，建议开发者将初始化事件放在主⼊⼝或前置。
        AdSdk.getInstance().setBaseHttp("https://ad.bwton.com");

        // 设置是否是debug模式
        AdSdk.getInstance().setDebug(!"NONE".equalsIgnoreCase(logType));

        // 设置是否需要打印日志
        LogUtil.setIsNeedLog(!"NONE".equalsIgnoreCase(logType));

        // 配置微信小程序
        if (!TextUtils.isEmpty(wechatAppId)) {
            AdSdk.getInstance().setWxAppId(wechatAppId);
        }


        // 初始化
        AdSdk.getInstance().init(context.getApplicationContext(), appId, result -> {
            bwtInited = true;
            bwtInitSuccess = result;
            if (bwtInitCallback != null) {
                bwtInitCallback.onInitResult(result);
            }
            LogUtil.log(result ? "init success!" : "init error!");
        });
        inited = true;
    }
}